package com.servlet.login.root.utils;


import java.sql.*;
import java.util.List;
import java.util.ResourceBundle;

public class DBUtils {
    private static Connection connection = null;
    private static PreparedStatement pps = null;
    private static ResultSet resultSet = null;
    //public var
    private static String driverName = null;
    private static String url = null;
    private static String user = null;
    private static String password = null;
    private static int updateCount = 0;


    //getter and setter
    public static Connection getConnection() {
        return connection;
    }

    public static void setConnection(Connection connection) {
        DBUtils.connection = connection;
    }

    public static PreparedStatement getStatement() {
        return pps;
    }

    public static void setStatement(PreparedStatement statement) {
        DBUtils.pps = statement;
    }

    public static ResultSet getResultSet() {
        return resultSet;
    }

    public static void setResultSet(ResultSet resultSet) {
        DBUtils.resultSet = resultSet;
    }

    //code block
    static {
        //            InputStream inputStream = DBUtils.class.getClassLoader()
//                    .getResourceAsStream("db.properties");
//            Properties properties = new Properties();
//            properties.load(inputStream);
//            driverName = properties.getProperty("diver");
//            url = properties.getProperty("url");
//            user = properties.getProperty("user");
//            password = properties.getProperty("password");
        try {
            ResourceBundle db = ResourceBundle.getBundle("db");
            driverName = db.getString("diver");
            user = db.getString("user");
            password = db.getString("password");
            url = db.getString("url");
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
//            System.out.println(url+driverName+user+password);
//        dds.setUsername(user);
//        dds.setPassword(password);
//        dds.setUrl(url);
//        dds.setDriverClassName(driverName);
    }


    //methods
    protected static Connection getconnection() {
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/servlet4login?serverTimezone=UTC", "root", "zhanjian1-1");
//            connection = dds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    protected static PreparedStatement getPps(String sql) {
        try {
            getconnection();
            pps = connection.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pps;
    }

    protected void setParams(List list) {
        try {
            if (null != list && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    pps.setObject(i + 1, list.get(i));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //6.增删改调取的方法
    protected int update(String sql, List params) {
        try {
            getPps(sql);
            setParams(params);
            updateCount = pps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return updateCount;
    }


    //7.查询的时候调取一个方法
    protected ResultSet query(String sql, List list) {
        try {
            getPps(sql);
            setParams(list);
            resultSet = pps.executeQuery();
            return resultSet;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }



    //8.关闭资源
    protected void closeall() {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (pps != null) {
                pps.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}



